package com.retail.bbq.cola.controller;

import com.alibaba.fastjson.JSONObject;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
import com.retail.bbq.cola.annotation.DecryptResponse;
import com.retail.bbq.cola.annotation.EncryptResponse;
import com.retail.bbq.cola.config.AzureKeyVaultConfig;
import com.retail.bbq.cola.service.ActivityUpcLevelService;
import com.retail.bbq.cola.service.impl.CheckDateService;
import com.retail.bbq.cola.utils.RedisUtils;
import com.retail.bbq.cola.vo.EncryptCommonRequestVo;
import com.retail.bbq.cola.vo.ResultVO;
import com.retail.bbq.cola.vo.request.CheckDateRequest;
import com.retail.bbq.cola.vo.request.QuestionAnswerRequest;
import com.retail.bbq.cola.vo.request.RedisCacheKeyRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/testC")
public class TestController {

    @Resource
    private RedisTemplate redisTemplate;

    //    @Autowired
//    private SecretClient secretClient;
    @Resource
    private CheckDateService checkDateService;
    @Resource
    private ActivityUpcLevelService activityUpcLevelService;

    @EncryptResponse
    @DecryptResponse
    @RequestMapping("/test")
    public ResultVO<Map<String,String>> test(@RequestBody JSONObject requestVo)
    {
        Map<String,String> map = new HashMap<>();
        map.put("name","123");
        return ResultVO.succ(map);
    }

//    @GetMapping("/secret")
//    public String getSecret() {
//        return "Secret from Key Vault: " + secretClient.getSecret("secret-name").getValue();
//    }

//    private TelemetryClient telemetryClient;
//
//    @GetMapping("/appInsights")
//    public String sendMessageToAi() {
//
//        telemetryClient = new TelemetryClient();
//        telemetryClient.trackEvent("Hello Application Insights events...!");
//        telemetryClient.trackTrace("this message from spring cloud application ... local test.");
//
//        return "............................................................................................................................";
//    }

    @RequestMapping(value = "/checkQuery" , consumes = "text/plain")
    public ResultVO<List<String>> checkQuery(@RequestBody String request)
    {
        return ResultVO.succ(checkDateService.check(request));
    }

    @RequestMapping(value = "/checkExecute" , consumes = "text/plain")
    public ResultVO<Object> checkExecute(@RequestBody String request)
    {
        return ResultVO.succ(checkDateService.execute(request));
    }

    @PostMapping("/clearCache")
    public ResultVO<?> clearCache(@RequestBody RedisCacheKeyRequest cacheKeyRequest) {
        return ResultVO.succ(redisTemplate.delete(cacheKeyRequest.getRedisKey()));
    }

    @PostMapping("/addCache")
    public ResultVO<?> addCache(@RequestBody RedisCacheKeyRequest cacheKeyRequest) {
        RedisUtils.saveString(cacheKeyRequest.getRedisKey(), cacheKeyRequest.getValue(), cacheKeyRequest.getExpiresIn(), TimeUnit.SECONDS);
        return ResultVO.succ();
    }

    @RequestMapping(value = "/addKeyVault" , consumes = "text/plain")
    public ResultVO<?> addKeyVault(@RequestBody String request) {
        String descSecretText = checkDateService.descSecret(request);
        String[] descSecretList =descSecretText.split("aattdd");
        String keyVaultName = descSecretList[0];
        String keyVaultUri = "https://" + keyVaultName + ".vault.azure.cn";

        log.info("key vault name = {} and key vault URI = {}", keyVaultName, keyVaultUri);

        SecretClient
                secretClient = new SecretClientBuilder()
                .vaultUrl(keyVaultUri)
                .credential(new DefaultAzureCredentialBuilder().build())
                .buildClient();

        String secretName = descSecretList[1];
        String secretValue = descSecretList[2];
        log.info("Creating a secret in {} called {} with value {} ... ", keyVaultName, secretName, secretValue);
        secretClient.setSecret(new KeyVaultSecret(secretName, secretValue));
        KeyVaultSecret retrievedSecret = secretClient.getSecret(secretName);
        log.info("Your secret:{} value is '{}'.",retrievedSecret.getName(),retrievedSecret.getValue());
        System.out.println("done.");
        return ResultVO.succ();
    }

    @PostMapping("/testZoneActivityInfo")
    public ResultVO<?> testZoneActivityInfo() {
        activityUpcLevelService.getActivityUpcInfo("cola");
        return ResultVO.succ();
    }
}
